<?php
if(!defined('IN_DISCUZ')) {
	exit('Access Denied');
}
class table_aljbd_goods extends discuz_table{
	public function __construct() {

			$this->_table = 'aljbd_goods';
			$this->_pk    = 'id';

			parent::__construct();
	}
	public function count_by_status($status=0,$uid=0,$type=0,$subtype=0,$region=0,$subregion=0,$search='',$rubbish=0){
		$con[]=$this->_table;
		$where='where 1 ';
		$con[]=$rubbish;
		$where.=' and rubbish=%d';
		if($uid){
			$con[]=$uid;
			$where.=' and uid=%d';
		}
		if($status){
			$con[]=$status;
			$where.=' and sign=%d';
		}
		if($type){
			$con[]=$type;
			$where.=' and type=%d';
		}
		if($subtype){
			$con[]=$subtype;
			$where.=' and subtype=%d';
		}
		if($region){
			$con[]=$region;
			$where.=' and region=%d';
		}
		if($subregion){
			$con[]=$subregion;
			$where.=' and subregion=%d';
		}
		if($search){
			$con[]='%'.addcslashes($search, '%_').'%';
			
			$where.=" and name like %s";
		}//debug('select count(*) from %t '.$where);
		return DB::result_first('select count(*) from %t '.$where,$con);
	}
	public function count_by_status_new($status=0,$uid=0,$type=0,$subtype=0,$region=0,$subregion=0,$search='',$subtype3=0,$rubbish=0){
		global $settings;
		$con[]=$this->_table;
		$where='where state =0';
		$con[]=$rubbish;
		$where.=' and rubbish=%d';
		if($uid){
			$con[]=$uid;
			$where.=' and uid=%d';
		}
		if($status){
			$con[]=$status;
			$where.=' and sign=%d';
		}
		if($type){
			$con[]=$type;
			$where.=' and type=%d';
		}
		if($subtype){
			$con[]=$subtype;
			$where.=' and subtype=%d';
		}
		if($subtype3){
			$con[]=$subtype3;
			$where.=' and subtype3=%d';
		}
		if($region){
			$con[]=$region;
			$where.=' and region=%d';
		}
		if($subregion){
			$con[]=$subregion;
			$where.=' and subregion=%d';
		}
		if($settings['displayexpiredgoods']['value']){
			$con[]=TIMESTAMP;
			$where.=' and (endtime>%d or endtime=0)';
		}
		if($search){
			$con[]='%'.addcslashes($search, '%_').'%';
			
			$where.=" and name like %s";
		}//debug('select count(*) from %t '.$where);
		return DB::result_first('select count(*) from %t '.$where,$con);
	}
	public function fetch_all_by_status($status=0,$start=0,$perpage=0,$uid=0,$type=0,$subtype=0,$region=0,$subregion=0,$order='',$search='',$rubbish=0){
		$con[]=$this->_table;
		$where='where 1 ';
		$con[]=$rubbish;
		$where.=' and rubbish=%d';
		if($status){
			$con[]=$status;
			$where.=' and sign=%d';
		}
		if($uid){
			$con[]=$uid;
			$where.=' and uid=%d';
		}
		if($type){
			$con[]=$type;
			$where.=' and type=%d';
		}
		if($subtype){
			$con[]=$subtype;
			$where.=' and subtype=%d';
		}
		if($region){
			$con[]=$region;
			$where.=' and region=%d';
		}
		if($subregion){
			$con[]=$subregion;
			$where.=' and subregion=%d';
		}
		if($search){
			$con[]='%'.addcslashes($search, '%_').'%';
			
			$where.=" and name like %s";
		}
		if($order){
			if($order=='price1'){
				$where.=' order by '.addslashes($order).' asc';
			}else{
				$where.=' order by '.addslashes($order).' desc';
			}
			
		}else{
			$where.=' order by displayorder desc,dateline desc,view desc';
		}
		if(!empty($perpage)){
			$con[]=$start;
			$con[]=$perpage;
			$where.=' limit %d,%d';
		}
		return DB::fetch_all('select * from %t '.$where,$con);
	}
	public function fetch_all_by_status_new($status=0,$start=0,$perpage=0,$uid=0,$type=0,$subtype=0,$region=0,$subregion=0,$order='',$search='',$subtype3=0,$rubbish=0){
		global $settings;
		$con[]=$this->_table;
		$where='where state =0';
		$con[]=$rubbish;
		$where.=' and rubbish=%d';
		if($status){
			$con[]=$status;
			$where.=' and sign=%d';
		}
		if($uid){
			$con[]=$uid;
			$where.=' and uid=%d';
		}
		if($type){
			$con[]=$type;
			$where.=' and type=%d';
		}
		if($subtype){
			$con[]=$subtype;
			$where.=' and subtype=%d';
		}
		if($subtype3){
			$con[]=$subtype3;
			$where.=' and subtype3=%d';
		}
		if($region){
			$con[]=$region;
			$where.=' and region=%d';
		}
		if($subregion){
			$con[]=$subregion;
			$where.=' and subregion=%d';
		}
		if($settings['displayexpiredgoods']['value']){
			$con[]=TIMESTAMP;
			$where.=' and (endtime>%d or endtime=0)';
		}
		if($search){
			$con[]='%'.addcslashes($search, '%_').'%';
			
			$where.=" and name like %s";
		}
		if($order){
			if($order=='price1'){
				$where.=' order by '.addslashes($order).' asc';
			}else{
				$where.=' order by '.addslashes($order).' desc';
			}
			
		}else{
			$where.=' order by displayorder desc,dateline desc,view desc';
		}
		if(!empty($perpage)){
			$con[]=$start;
			$con[]=$perpage;
			$where.=' limit %d,%d';
		}
		
		return DB::fetch_all('select * from %t '.$where,$con);
	}
	public function count_by_type(){
		return DB::fetch_all('select type,count(*) num from %t group by type',array($this->_table));
	}
	public function count_by_uid_bid($uid=0,$bid=0,$rubbish=0,$search=''){
		$conn=' where 1';
		$where[]=$this->_table;
		$where[]=$rubbish;
		$conn.=' and rubbish=%d';
		if($uid){
			$where[]=$uid;
			$conn.=' and uid=%d';
		}
		if($bid){
			$where[]=$bid;
			$conn.=' and bid=%d';
		}
        if($search){
            $where[]='%'.addcslashes($search, '%_').'%';

            $conn.=" and name like %s";
        }
		return DB::result_first('select count(*) from %t '.$conn,$where);
	}
    public function fetch_all_by_uid_bid($uid=0,$bid=0,$start=0,$perpage=0,$rubbish=0,$search=''){
        $conn=' where 1';
        $where[]=$this->_table;
        $where[]=$rubbish;
        $conn.=' and rubbish=%d';
        if($uid){
            $where[]=$uid;
            $conn.=' and uid=%d';
        }
        if($bid){
            $where[]=$bid;
            $conn.=' and bid=%d';
        }

        if($search){
            $where[]='%'.addcslashes($search, '%_').'%';

            $conn.=" and name like %s";
        }

        $conn.=' order by displayorder desc,id desc';
        if(isset($start)&&isset($perpage)){
            $where[]=$start;
            $where[]=$perpage;
            $conn.=' limit %d,%d';
        }
        return DB::fetch_all('select * from %t '.$conn,$where);
    }
	public function count_by_uid_bid_new($uid=0,$bid=0,$rubbish=0){
		global $settings;
		$conn=' where state =0';
		$where[]=$this->_table;
		$where[]=$rubbish;
		$conn.=' and rubbish=%d';
		if($uid){
			$where[]=$uid;
			$conn.=' and uid=%d';
		}
		if($bid){
			$where[]=$bid;
			$conn.=' and bid=%d';
		}
		if($settings['displayexpiredgoods']['value']){
			$where[]=TIMESTAMP;
			$conn.=' and (endtime>%d or endtime=0)';
		}
		return DB::result_first('select count(*) from %t '.$conn,$where);
	}

	public function fetch_all_by_uid_bid_new($uid=0,$bid=0,$start=0,$perpage=0,$rubbish=0){
		global $settings;
		$conn=' where state =0';
		$where[]=$this->_table;
		$where[]=$rubbish;
		$conn.=' and rubbish=%d';
		if($uid){
			$where[]=$uid;
			$conn.=' and uid=%d';
		}
		if($bid){
			$where[]=$bid;
			$conn.=' and bid=%d';
		}
		if($settings['displayexpiredgoods']['value']){
			$where[]=TIMESTAMP;
			$conn.=' and (endtime>%d or endtime=0)';
		}
		$conn.=' order by displayorder desc,id desc';
		if(isset($start)&&isset($perpage)){
			$where[]=$start;
			$where[]=$perpage;
			$conn.=' limit %d,%d';
		}
		return DB::fetch_all('select * from %t '.$conn,$where);
	}
	public function fetch_all_by_uid_bid_view($uid=0,$bid=0,$start=0,$perpage=0,$rubbish=0){
		$conn=' where 1';
		$where[]=$this->_table;
		$where[]=$rubbish;
		$conn.=' and rubbish=%d';
		if($uid){
			$where[]=$uid;
			$conn.=' and uid=%d';
		}
		if($bid){
			$where[]=$bid;
			$conn.=' and bid=%d';
		}
		$conn.=' order by view desc';
		if(isset($start)&&isset($perpage)){
			$where[]=$start;
			$where[]=$perpage;
			$conn.=' limit %d,%d';
		}
		return DB::fetch_all('select * from %t '.$conn,$where);
	}
	public function fetch_all_by_uid_bid_view_new($uid=0,$bid=0,$start=0,$perpage=0,$rubbish=0){
		global $settings;
		$conn=' where state =0';
		$where[]=$this->_table;
		$where[]=$rubbish;
		$conn.=' and rubbish=%d';
		if($uid){
			$where[]=$uid;
			$conn.=' and uid=%d';
		}
		if($bid){
			$where[]=$bid;
			$conn.=' and bid=%d';
		}
		if($settings['displayexpiredgoods']['value']){
			$where[]=TIMESTAMP;
			$conn.=' and (endtime>%d or endtime=0)';
		}
		$conn.=' order by view desc';
		if(isset($start)&&isset($perpage)){
			$where[]=$start;
			$where[]=$perpage;
			$conn.=' limit %d,%d';
		}
		return DB::fetch_all('select * from %t '.$conn,$where);
	}
	public function update_view_by_gid($gid=0){
		return DB::query('update %t set view=view+1 where id=%d',array($this->_table,$gid));
	}
	public function fetch_thread_all_block($con='',$sc='',$items=0){
		return DB::fetch_all("select * from %t $con $sc limit 0,%d",array($this->_table,$items));
	}
	public function fetch_all_by_recommend($recommend=0,$start=0,$perpage=0){
		return DB::fetch_all('select * from %t where sign=%d and rubbish=0 order by displayorder desc limit %d,%d',array($this->_table,$recommend,$start,$perpage));
	}
	public function fetch_all_by_recommend_new($recommend=0,$start=0,$perpage=0){
		global $settings;
		if($settings['displayexpiredgoods']['value']){
			return DB::fetch_all('select * from %t where sign=%d and rubbish=0 and (endtime>%d or endtime=0)  order by displayorder desc limit %d,%d',array($this->_table,$recommend,TIMESTAMP,$start,$perpage));
		}else{
			return DB::fetch_all('select * from %t where sign=%d and rubbish=0 order by displayorder desc limit %d,%d',array($this->_table,$recommend,$start,$perpage));
		}
		
	}
	public function update_num_by_id($id=0,$num=0) {
        return DB::query('update %t set amount=amount-%d where id=%d', array($this->_table,$num,$id));
    }
    public function update_num2_by_id($id=0,$num=0) {
        return DB::query('update %t set buyamount=buyamount+%d where id=%d', array($this->_table,$num,$id));
    }
	public function update_displayorder_by_id($id=0,$displayorder=0){
		return DB::query('update %t set displayorder=%d where id=%d',array($this->_table,$displayorder,$id));
	}
}




?>